<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *活动管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择拼团活动！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	} 
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_aligroup_groups',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_aligroup/goods//id='.$item['id'],true);
			$item['goodstitle']=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']));
			$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']));
			if($item['agio']!=0){
				$item['groupsprice']=$goods['price']*$item['agio']/100;
			}else{
				$item['groupsprice']=$goods['price'];
			}
			$item['price']=$goods['price'];
			$item['endstatus']=$item['starttime']<time() ? $item['endtime']>time()? 1 : 2 : 0;
			$item['statustitle']=$item['starttime']<time() ? $item['endtime']>time()? '去拼团' : '已结束' : '未开始';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'活动列表'),
			array('op'=>'edit','title'=>'添加活动'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'活动名称','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'agio','title'=>'折扣','type'=>'string'),
			array('name'=>'singleprice','title'=>'单购价','type'=>'string'),
			array('name'=>'timeout','title'=>'组团限时/小时','type'=>'string'),
			array('name'=>'starttime','title'=>'开始时间','type'=>'time'),
			array('name'=>'endtime','title'=>'结束时间','type'=>'time'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			array('name'=>'isindex','title'=>'是否首页热门','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'活动名称','type'=>'string','width'=>'20'),
			array('name'=>'status','title'=>'是否启用','type'=>'select','options'=>'status','width'=>'20'),
			
		)
	);
	$return['status']=array(array('id'=>'0','title'=>'禁用'),array('id'=>'1','title'=>'启用'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aligroup_groups',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['starttime']=$item['starttime']?date('Y-m-d H:i',$item['starttime']):'';
	$item['endtime']=$item['endtime']?date('Y-m-d H:i',$item['endtime']):'';
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回活动列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('type'=>'string','title'=>'活动名称','name'=>'title','help'=>''),
								array('type'=>'text','title'=>'活动描述','name'=>'desc','help'=>''),
								array('type'=>'image','title'=>'活动图片','name'=>'image','help'=>''),
								array('name'=>'categoryid','title'=>'选择分类','type'=>'select','options'=>'category'),
								array('name'=>'goodsid','title'=>'组内商品','type'=>'choose','action'=>'jy_weishop/goods.get_goods/nopluginname=1'),
								array('type'=>'string','title'=>'商品自定义数量','name'=>'goodsnum','help'=>''),
								array('type'=>'switch','title'=>'价格模式？','name'=>'feetype','help'=>'开启后，直接指定拼团价格！如果是多规格商品，建议使用折扣模式！'),
								array('type'=>'unitstring','title'=>'拼团折扣','name'=>'agio','unit'=>'%','show'=>array('feetype'=>0)),
								array('type'=>'unitstring','title'=>'拼团价格','name'=>'price','unit'=>'元','show'=>array('feetype'=>1)),
								array('type'=>'string','title'=>'成团人数','name'=>'groupnum','help'=>''),
								array('type'=>'string','title'=>'虚拟已成团数','name'=>'teamnum','help'=>''),
								array('name'=>'starttime','title'=>'开始时间','type'=>'time'),
								array('name'=>'endtime','title'=>'结束时间','type'=>'time'),
								array('type'=>'string','title'=>'组团限时','name'=>'timeout','help'=>'仅填数字，单位:小时'),
								array('type'=>'string','title'=>'虚拟已出售数量','name'=>'sales','help'=>''),
								array('type'=>'string','title'=>'排序','name'=>'displayorder','help'=>'填写数字，按从小到大排序展示'),
								array('type'=>'switch','title'=>'首页热门','name'=>'isindex','help'=>'设置是否首页展示'),
								//array('type'=>'switch','title'=>'是否热门','name'=>'ishot','help'=>''),
								array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
								array('type'=>'switch','title'=>'支持单购','name'=>'single','help'=>'设置是否启用'),
								array('name'=>'no_paytype','title'=>'不支持的支付方式','type'=>'checkbox','options'=>'paytypes','help'=>'勾选后，只要订单包含该商品，就生效！'),
								array('type'=>'string','title'=>'单独购买价格','name'=>'singleprice','help'=>''),
								array('type'=>'radio','title'=>'是否单独设置团长优惠','name'=>'isdiscount','options'=>'isdiscount','help'=>''),
								array('type'=>'radio','title'=>'团长优惠方式','name'=>'headstype','help'=>'','options'=>'headstype','show'=>array('isdiscount'=>1)),
								array('type'=>'string','title'=>'优惠金额','name'=>'headsmoney','help'=>'','show'=>array('isdiscount'=>1,'headstype'=>1),'help'=>'优惠金额：例如：10元，总价-10元！'),
								array('type'=>'string','title'=>'优惠折扣','name'=>'headsdiscount','help'=>'','show'=>array('isdiscount'=>1,'headstype'=>2),'help'=>'总价-(总价*折扣/100)，例如：80就等于8折'),
								
							)
						),
			
			)
	);
	$get_pay_types=get_pay_types();
	unset($get_pay_types['apipay'],$get_pay_types['yuejie'],$get_pay_types['credit3'],$get_pay_types['credit4'],$get_pay_types['credit5'],$get_pay_types['credit6']);
	$return['paytypes']=$get_pay_types;
	$return['isdiscount']=array('0'=>'否','1'=>'是');
	$return['headstype']=array('1'=>'优惠金额','2'=>'优惠折扣');
	$return['category']=pdo_getall('jy_aligroup_groups_category',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['title'])){
		exi('请完善活动名称','error');
	}
	if(empty($postdata['image'])){
		exi('请完善活动图片','error');
	}
	if(!$postdata['categoryid']){
		exi('请选择活动分类','error');
	}
	if(!$postdata['goodsid']){
		exi('请选择商品','error');
	}
	if(!$postdata['groupnum'] || $postdata['groupnum']<2){
		exi('成团人数必须大于等于2','error');
	}
	if(!$postdata['feetype'] && $postdata['agio']>=100){
		exi('折扣范围错误，请重新设置','error');
	}
	if($postdata['feetype'] && $postdata['price']<=0){
		exi('拼团价格必填','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'desc'=>trim($postdata['desc']),
		'image'=>trim($postdata['image']),
		'goodsid'=>intval($postdata['goodsid']),
		'categoryid'=>intval($postdata['categoryid']),
		'goodsnum'=>intval($postdata['goodsnum']),
		'agio'=>intval($postdata['agio']),
		'groupnum'=>intval($postdata['groupnum']),
		'teamnum'=>intval($postdata['teamnum']),
		'timeout'=>intval($postdata['timeout']),
		'sales'=>intval($postdata['sales']),
		'displayorder'=>intval($postdata['displayorder']),
		'isindex'=>intval($postdata['isindex']),
		'ishot'=>intval($postdata['ishot']),
		'single'=>intval($postdata['single']),
		'singleprice'=>intval($postdata['singleprice']),
		'isdiscount'=>intval($postdata['isdiscount']),
		'status'=>intval($postdata['status']),
		'headstype'=>intval($postdata['headstype']),
		'headsmoney'=>intval($postdata['headsmoney']),
		'headsdiscount'=>intval($postdata['headsdiscount']),
		'endtime'=>intval(strtotime($postdata['endtime'])),
		'starttime'=>intval(strtotime($postdata['starttime'])),
		'feetype'=>intval($postdata['feetype']),
		'price'=>trim($postdata['price']),
		'no_paytype'=>trim($postdata['no_paytype']),
		'createtime'=>TIMESTAMP,
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_aligroup_groups',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_aligroup_groups',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_aligroup_groups',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的活动！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_aligroup_groups',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		pdo_update('jy_aligroup_groups',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['single'])){
		pdo_update('jy_aligroup_groups',array('single'=>intval($post['single'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isindex'])){
		pdo_update('jy_aligroup_groups',array('isindex'=>intval($post['isindex'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
